package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.GoodsMapper;
import com.itheima.entity.Result;
import com.itheima.pojo.Goods;
import com.itheima.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;


    //查询所有
    @Override
    public Result findAll() {
        List<Goods> goodsList = this.list();
        return new Result(true,null,goodsList);
    }



    //添加
    @Override
    public Result add(Goods good) {
        boolean isSuccess = this.save(good);
        if (isSuccess) {
            return new Result(true, null, "插入成功");
        }
        return new Result(false,null,"插入失败");
    }

    @Override
    //查询分页
    public Result findPage( @PathVariable("page") Integer page,@PathVariable("size") Integer size) {//前端的参数 pagea sizea
        Page<Goods> page1 = new Page<>(page, size);
        this.page(page1);
        List<Goods> records = page1.getRecords();
        return new Result(true,null,records);
    }

    @Override
    //删除
    public Result delete(Integer id) {
        boolean isSuccess = this.removeById(id);
        if (!isSuccess) {
            return new Result(false,null,null);
        }
        return new Result(true,null,null);
    }

    @Override
    //通过id查询
    public Result findById(@PathVariable("id") Integer id) {
        Goods goods = this.getById(id);
        if (goods==null) {
            return new Result(false,null,null);
        }
        return new Result(true,null,goods);
    }

    @Override
    //更新
    public Result update(Integer id, Goods goods) {
        boolean isSuccess = this.updateById(goods);
        if (!isSuccess) {
            return new Result(false,null,null);
        }
        return new Result(true,null,null);
    }
}
